<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Comment as CommentModel;
use addons\qingdongams\model\Aftermarket;
use addons\qingdongams\model\Message;
use addons\qingdongams\model\Event;
use addons\qingdongams\model\StaffSignIn;
use addons\qingdongams\model\WorkReport;
use think\Db;
use think\Exception;
use function EasyWeChat\Kernel\Support\get_client_ip;

/**
 * 评论
 */
class Comment extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];
    public function _initialize()
    {
        parent::_initialize();
        try{
            \think\Db::execute("SET @@sql_mode='';");
        }catch (Exception $e){

        }
    }
    //添加评论
    public function addComment()
    {
        $content = input('content');
        $files = input('file_ids');
        $record_id = input('record_id');
        $relation_type = input('relation_type', 'sign');
        if (empty($content)) {
            $this->error('评论内容不能为空');
        }
        $data = [
            'relation_type' => $relation_type,
            'relation_id' => $record_id,
            'staff_id' => $this->auth->id,
            'content' => $content,
            'file_ids' => $files,
            'status' => 1,
            'ip' => get_client_ip(),
        ];


        $commentModel = new CommentModel();
        Db::startTrans();
        try {
            $commentModel->save($data);
            if ($relation_type ==$commentModel::SIGN_TYPE) {
                $record = StaffSignIn::where(['id' => $record_id])->find();
                $record->create_staff_id=$record['staff_id'];
                $type = Message::RECORD_TYPE;
                $content = $this->auth->name . '评论了您提交的跟进记录!';
                $content2 = $this->auth->name . '评论了您关注的跟进记录!';
            } elseif ($relation_type ==$commentModel::EVENT_TYPE) {
                $record = Event::where(['id' => $record_id])->find();
                $type = Message::EVENT_TYPE;
                $content = $this->auth->name . '评论了您的日程!';
                $content2 = $this->auth->name . '评论了您的日程!';
            } elseif ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
                $record = Event::where(['id' => $record_id])->find();
                $type = Message::ASSIST_EVENT_TYPE;
                $content = $this->auth->name . '评论了您的协助任务!';
                $content2 = $this->auth->name . '评论了您的协助任务!';
            } elseif($relation_type ==$commentModel::ARTICLE_TYPE) {
                Aftermarket::where(['id'=>$record_id])->setInc('comment_num');
                $record = Aftermarket::where(['id' => $record_id])->find();
                $type = Message::ARTICLE_TYPE;
                $content = $this->auth->name . '增加了文章评论!';
                $content2 = $this->auth->name . '增加了文章评论!';
            }elseif($relation_type ==$commentModel::WORKREPORT_TYPE) {
                $record = WorkReport::where(['id' => $record_id])->find();
                $type = Message::WORKREPORT_TYPE;
                $content = $this->auth->name . '评论了您的工作报告!';
                $content2 = $this->auth->name . '评论了您关注的工作报告!';
                $record['create_staff_id']=$record['staff_id'];
            }elseif($relation_type ==$commentModel::WORKORDER_TYPE) {
                $record = \addons\qingdongams\model\Workorder::where(['id' => $record_id])->find();
                $type = Message::WORKORDER_TYPE;
                $content = $this->auth->name . '评论了您的工单!';
                $content2 = $this->auth->name . '评论了您关注的工单!';
            }elseif($relation_type ==$commentModel::DISCUSS_TYPE) {
                $record = \addons\qingdongams\model\Discuss::where(['id' => $record_id])->find();
                $type = Message::DISCUSS_TYPE;
                $content = $this->auth->name . '评论了您的讨论!';
                $content2 = $this->auth->name . '评论了您关注的讨论!';
            }
            if(empty($record)){
                $this->error('记录不存在');
            }
            if ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
                Message::addMessage($type, $record_id, $record['create_staff_id'], $this->auth->id, $content);
                Message::addMessage($type, $record_id, $record['staff_id'], $this->auth->id, $content2);
            }else{
                Message::addMessage($type, $record_id, $record['create_staff_id'], $this->auth->id, $content);
                $staff_ids = $commentModel->where(['relation_type' => $relation_type, 'relation_id' => $record_id])->group('staff_id')->column('staff_id');
                foreach ($staff_ids as $staff_id) {
                    //发送通知
                    if ($staff_id != $this->auth->id) {
                        Message::addMessage($type, $record_id, $staff_id, $this->auth->id, $content2);
                    }
                }
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('评论成功');
    }


    //评论列表
    public function commentList()
    {
        $record_id = input('record_id');
        $relation_type = input('relation_type', 'sign');

        $comments = CommentModel::where([
            'relation_type' => $relation_type,
            'relation_id' => $record_id,
            'status' => 1
        ])->field('id,staff_id,content,createtime')->order('id desc')->with(['staff'])->select();
        $this->success('请求成功', $comments);
    }

    // 文章评论点赞
    public function goodComment(){
        $id = input('id');
        if(!$id){
            $this->error('评论id呢？');
        }
        $commentModel = new CommentModel();
        $row = $commentModel->get($id);
        if(!$row){
            $this->error('评论不存在');
        }
        if($row->setInc('praise')){
            $this->success('成功');
        }else{
            $this->error('失败');
        }

    }

}
